﻿using Manon.Autofac;
using Manon.Core.Commons;
using Manon.Gateway.ApplicationCore.Dtos.GlobalConfiguration;
using Manon.Gateway.ApplicationCore.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Manon.Gateway.Admin.Controllers
{
    public class GlobalConfigurationController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Edit()
        {
            return View();
        }


        /// <summary>
        /// 保存全局设置
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> SaveGlobalConfiguration([FromBody] SaveGlobalConfigurationInput Input)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IGlobalConfigurationService>();
                return await routeService.SaveGlobalConfiguration(Input);
            }
            catch (Exception ex)
            {
                logger.LogError($"保存全局设置异常,参数Input={JsonConvert.SerializeObject(Input)} ", ex);
                throw;
            }
        }

        /// <summary>
        /// 获取全局设置
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<GetGlobalConfigurationOutput> GetGlobalConfiguration(int Id)
        {
            var logger = Resolver.Resolve<ILogger<RoutesController>>();
            try
            {
                var routeService = Resolver.Resolve<IGlobalConfigurationService>();
                return await routeService.GetGlobalConfiguration();
            }
            catch (Exception ex)
            {
                logger.LogError($"获取全局设置异常,参数Id={Id} ", ex);
                throw;
            }

        }
    }
}
